package com.loe.mvp.util

import android.content.Context
import android.view.Gravity
import com.loe.mvp.R
import com.loe.mvp.ext_app.addOnItemClickListener
import com.loe.mvp.ext_app.createAdapter
import com.loe.mvp.ext_app.px
import com.loe.mvp.ext_java.safe
import kotlinx.android.synthetic.main.popup_base_list.view.*
import kotlinx.android.synthetic.main.popup_base_list_item.view.*

class PopuperList
@JvmOverloads
constructor(context: Context, width: Int = WRAP_CONTENT, height: Int = WRAP_CONTENT) : Popuper(context, R.layout.popup_base_list, width, height)
{
    var gravity = Gravity.CENTER
    var paddingH = px(15)
        set(value)
        {
            field = px(value)
        }
    var paddingV = px(6)
        set(value)
        {
            field = px(value)
        }
    var textSize = 12

    var radius = px(2)
        set(value)
        {
            field = px(value)
            safe {
                root.cardView.radius = field.toFloat()
            }
        }

    val adapter = createAdapter<CodeBean>(R.layout.popup_base_list_item)
    { holder, bean ->
        textView.text = bean.name
        textView.gravity = gravity
        textView.setPadding(paddingH, paddingV, paddingH, paddingV)
        textView.textSize = textSize.toFloat()
    }

    init
    {
        run()
        {
            recyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
            recyclerView.adapter = adapter

            recyclerView.addOnItemClickListener()
            {
                val bean = adapter[it]
                onSelect?.invoke(bean)
                dismiss()
            }
            cardView.radius = radius.toFloat()
        }
    }

    fun gravity(value: Int): PopuperList
    {
        gravity = value
        return this
    }

    fun paddingV(value: Int): PopuperList
    {
        paddingV = value
        return this
    }

    fun paddingH(value: Int): PopuperList
    {
        paddingH = value
        return this
    }

    fun radius(value: Int): PopuperList
    {
        radius = value
        return this
    }

    fun textSize(value: Int): PopuperList
    {
        textSize = value
        return this
    }

    fun setList(list: List<CodeBean>): PopuperList
    {
        adapter.setNewData(list)
        return this
    }

    fun setStringList(list: List<String>): PopuperList
    {
        adapter.setNewData(list.map { CodeBean(it) })
        return this
    }

    fun setStrings(vararg s: String): PopuperList
    {
        setStringList(s.toList())
        return this
    }

    private var onSelect: ((bean: CodeBean) -> Unit)? = null

    fun setOnSelectListener(onSelect: (bean: CodeBean) -> Unit): PopuperList
    {
        this.onSelect = onSelect
        return this
    }
}

class CodeBean
{
    var name: String = ""
    var code: String = ""

    constructor(name: String)
    {
        this.name = name
        this.code = name
    }

    constructor(name: String, code: String)
    {
        this.name = name
        this.code = code
    }
}